package com.quectel.core.module.devicepatrol.service.impl;


import com.quectel.constant.global.SystemConstants;
import com.quectel.core.constants.RedisCacheConstants;
import com.quectel.core.module.devicepatrol.dao.DevicePatrolPlanDao;
import com.quectel.core.module.devicepatrol.dto.DevicePatrolPlanDto;
import com.quectel.core.module.devicepatrol.dto.DevicePatrolRouteDto;
import com.quectel.core.module.devicepatrol.entity.DevicePatrolPlanEntity;
import com.quectel.core.module.devicepatrol.service.DevicePatrolPlanService;
import com.quectel.core.module.devicepatrol.service.DevicePatrolRouteService;
import com.quectel.util.common.CopyUtils;
import com.quectel.util.common.DateUtils;
import com.quectel.util.kit.CacheKit;
import com.quectel.util.kit.Snowflake;
import com.quectel.util.mybatis.MyBatisPlusUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.function.Function;

/**
 * @author caolu
 * @email louis.cao@quectel.com
 * @date 2022/05/07 14:07
 */
@DubboService
public class DevicePatrolPlanServiceImpl implements DevicePatrolPlanService {

    private static final Logger LOGGER = LoggerFactory.getLogger(DevicePatrolPlanServiceImpl.class);

    /**
     * 入参解释:实体id
     */
    private static final Function<Long, String> DEVICE_PATROL_PLAN_CACHE_BY_ID_FUNC = id -> String.format(RedisCacheConstants.PROJECT_NAME + "DEVICE_PATROL_PLAN_CACHE_BY_ID:%d", id);

    @Autowired
    private DevicePatrolPlanDao devicePatrolPlanDao;
    @Autowired
    private DevicePatrolRouteService devicePatrolRouteService;


    @Override
    public List<DevicePatrolPlanDto> queryListByDate(Date date) {
        int weekNum = DateUtils.getWeekNum(date);
        List<DevicePatrolPlanEntity> list = devicePatrolPlanDao.queryListByDateAndWeek(date, "\"" + weekNum + "\"");
        List<DevicePatrolPlanDto> result = CopyUtils.copyList(list, DevicePatrolPlanDto.class);
        result.forEach(this::paddingField);
        return result;
    }

    @Override
    public DevicePatrolPlanDto selectCacheById(Long id) {
        return CacheKit.cacheToRedis(
                () -> selectById(id),
                DEVICE_PATROL_PLAN_CACHE_BY_ID_FUNC.apply(id),
                SystemConstants.NOT_NULL_CACHE_EXPIRE_SECONDS
        );
    }

    @Override
    public DevicePatrolPlanDto selectById(Long id) {
        DevicePatrolPlanEntity entity = devicePatrolPlanDao.selectById(id);
        DevicePatrolPlanDto devicePatrolPlanDto = paddingField(CopyUtils.copyObj(entity, DevicePatrolPlanDto.class));

        if (devicePatrolPlanDto != null && devicePatrolPlanDto.getRouteId() != null) {
            DevicePatrolRouteDto devicePatrolRouteDto = devicePatrolRouteService.selectCacheById(devicePatrolPlanDto.getRouteId());
            devicePatrolPlanDto.setDevicePatrolRouteDto(devicePatrolRouteDto);
        }

        return devicePatrolPlanDto;
    }

    @Override
    public List<DevicePatrolPlanDto> queryList(Map<String, Object> params) {
        List<DevicePatrolPlanEntity> list = devicePatrolPlanDao.queryList(params);
        List<DevicePatrolPlanDto> result = CopyUtils.copyList(list, DevicePatrolPlanDto.class);
        result.forEach(this::paddingField);
        return result;
    }

    private DevicePatrolPlanDto paddingField(DevicePatrolPlanDto dto) {
        if (dto != null) {
            DevicePatrolRouteDto devicePatrolRouteDto = devicePatrolRouteService.selectCacheById(dto.getRouteId());
            if (devicePatrolRouteDto != null) {
                dto.setDevicePatrolRouteName(devicePatrolRouteDto.getName());
                dto.setVillageName(devicePatrolRouteDto.getVillageName());
            }
        }
        return dto;
    }

    @Override
    public int queryTotal(Map<String, Object> params) {
        return devicePatrolPlanDao.queryTotal(params);
    }

    @Override
    public Long save(DevicePatrolPlanDto dto) {
        DevicePatrolPlanEntity entity = CopyUtils.copyObj(dto, DevicePatrolPlanEntity.class);
        entity.setId(Snowflake.nextId());
        devicePatrolPlanDao.insert(entity);
        return entity.getId();
    }

    @Override
    public void updateById(DevicePatrolPlanDto dto) {
        DevicePatrolPlanEntity entity = CopyUtils.copyObj(dto, DevicePatrolPlanEntity.class);
        devicePatrolPlanDao.updateById(entity);
        CacheKit.invalidRedisCache(DEVICE_PATROL_PLAN_CACHE_BY_ID_FUNC.apply(dto.getId()));
    }

    @Override
    public void updateAllColumnById(DevicePatrolPlanDto dto) {
        DevicePatrolPlanEntity entity = CopyUtils.copyObj(dto, DevicePatrolPlanEntity.class);


        MyBatisPlusUtils.updateAllColumnById(entity, devicePatrolPlanDao);

        CacheKit.invalidRedisCache(DEVICE_PATROL_PLAN_CACHE_BY_ID_FUNC.apply(dto.getId()));
    }

    @Override
    public void deleteById(Long id) {
        devicePatrolPlanDao.deleteById(id);

        CacheKit.invalidRedisCache(DEVICE_PATROL_PLAN_CACHE_BY_ID_FUNC.apply(id));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteBatch(Long[] ids) {
        for (Long id : ids) {
            deleteById(id);
        }
    }
}
